﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;

namespace RealtorsPortal.AdminCP
{
    public partial class ManagePackages : System.Web.UI.Page
    {

        ParkagesDAL cDAL = new ParkagesDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPackageList();
            }
        }

        protected void LoadPackageList()
        {
            cDAL = new ParkagesDAL();
            grdPackagesList.DataSource = cDAL.getPackagesData();
            grdPackagesList.DataBind();
        }


        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdPackagesList.PageSize = int.Parse(ddlPageSize.Text);
            this.LoadPackageList();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int temp1;
            double temp2;
            Package p = new Package();
            p.PackageName = txtPackageName.Text;

            double.TryParse(txtPrice.Text, out temp2);
            p.Price = temp2;


            int.TryParse(txtNumOfAds.Text, out temp1);
            p.NumOfAds = temp1;

            int.TryParse(txtNumOfImg.Text, out temp1);
            p.NumOfImg = temp1;

            int.TryParse(txtDuration.Text, out temp1);
            p.Duration = temp1;


            p.IsActive = chkActived.Checked;
            p.IsVIP = chkIsVIP.Checked;
            if (!AddPackage(p))
            {
                lblStatus.Text = "Successed";
                LoadPackageList();
            }
            else
            {
                lblStatus.Text = "Error, check your typed in information";
                LoadPackageList();
            }
        }




        protected bool AddPackage(Package p)
        {
            if (cDAL.InsertPackage(p))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        protected void grdPackagesList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdPackagesList.EditIndex = e.NewEditIndex;
            LoadPackageList();
        }

        protected void grdPackagesList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdPackagesList.EditIndex = -1;
            LoadPackageList();
        }

        protected void grdPackagesList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != null)
            {
                int index = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {
                    case "UpdateCommand":
                        UpdatePackage(index);
                        grdPackagesList.EditIndex = -1;
                        LoadPackageList();
                        break;
                    case "DeleteCommand":
                        int PackageId = int.Parse(grdPackagesList.DataKeys[index].Value.ToString());
                        DeletePackage(PackageId);
                        grdPackagesList.EditIndex = -1;
                        LoadPackageList();
                        break;
                }
            }
        }

        protected bool DeletePackage(int pacId)
        {
            if (cDAL.DeletePackage(pacId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        protected bool UpdatePackage(int RowIndex)
        {
            int temp1;
            double temp2;

            Package p = new Package();
            p.PackageId = int.Parse(grdPackagesList.DataKeys[RowIndex].Value.ToString());
            p.PackageName = ((TextBox)grdPackagesList.Rows[RowIndex].FindControl("txtPackageName")).Text;

            double.TryParse((((TextBox)grdPackagesList.Rows[RowIndex].FindControl("txtPrice")).Text),out temp2);
            p.Price = temp2 ;

            int.TryParse((((TextBox)grdPackagesList.Rows[RowIndex].FindControl("txtDuration")).Text), out temp1);
            p.Duration = temp1;

            int.TryParse((((TextBox)grdPackagesList.Rows[RowIndex].FindControl("txtNumOfAds")).Text), out temp1);
            p.NumOfAds = temp1;

            int.TryParse((((TextBox)grdPackagesList.Rows[RowIndex].FindControl("txtNumOfImg")).Text), out temp1);
            p.NumOfImg = temp1;

            p.IsActive = ((CheckBox)grdPackagesList.Rows[RowIndex].FindControl("chkIsActive_edit")).Checked;
            p.IsVIP = ((CheckBox)grdPackagesList.Rows[RowIndex].FindControl("chkIsVIP_edit")).Checked;

            if (cDAL.UpdatePackage(p, p.PackageId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}